<template>
  <div>
    <H1>商户审核</H1>
  </div>
  <div>
    <el-form :inline="true" :model="from" class="demo-form-inline">
      <el-form-item label="关键字">
        <el-input v-model="from.keyWord" placeholder="请输入关键字" clearable />
      </el-form-item>
      <el-form-item label="性别">
        <el-select
          v-model="from.gender"
          placeholder="请选择"
          clearable
          style="width: 100px"
        >
          <el-option label="男" value="0" />
          <el-option label="女" value="1" />
        </el-select>
      </el-form-item>
      <el-form-item label="开始时间">
        <el-date-picker
          v-model="from.beginTime"
          type="date"
          placeholder="请选择"
          clearable
        />
      </el-form-item>
      <el-form-item label="结束时间">
        <el-date-picker
          v-model="from.endTime"
          type="date"
          placeholder="请选择"
          clearable
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="listHh()">查询</el-button>
      </el-form-item>
    </el-form>
  </div>
  <div></div>
  <div>
    <el-table
      :data="tableData"
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column prop="userId" label="id" width="180" />
      <el-table-column prop="userName" label="用户名" width="180" />
      <el-table-column prop="nickName" label="真实姓名" />
      <el-table-column prop="gender" label="性别">
        <template #default="scope">
          <el-tag type="primary" v-if="scope.row.gender == 0">男</el-tag>
          <el-tag type="success" v-if="scope.row.gender == 1">女</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="userStatus" label="状态">
        <template #default="scope">
          <el-tag type="primary" v-if="scope.row.userStatus == 0"
            >待审核</el-tag
          >
          <el-tag type="success" v-if="scope.row.userStatus == 1"
            >审核通过</el-tag
          >
          <el-tag type="info" v-if="scope.row.userStatus == 2">审核驳回</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="userMobile" label="电话" />
      <el-table-column prop="userEmail" label="邮箱" />
      <el-table-column prop="userAvatar" label="头像">
        <template #default="scope">
          <el-avatar shape="square" :size="100" :src="scope.row.userAvatar" />
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="注册时间" />
      <el-table-column label="操作">
        <template #default="scope">
          <el-button type="primary" @click="openUpdate(scope.row)"
            >审核</el-button
          >
        </template>
      </el-table-column>
    </el-table>
  </div>
  <div>
    <div class="demo-pagination-block">
      <el-pagination
        v-model:current-page="from.pageNum"
        v-model:page-size="from.pageSize"
        :page-sizes="[2, 4, 6, 8]"
        :size="size"
        :disabled="disabled"
        :background="background"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
  <div>
    <el-dialog v-model="dialogFormVisible" title="修改弹出框" width="500">
      <el-form :model="form">
        <el-form-item label="登录账号" :label-width="formLabelWidth">
          <el-input v-model="form.userName" autocomplete="off" disabled />
        </el-form-item>

        <el-form-item label="性别" :label-width="formLabelWidth">
          <el-select v-model="form.gender" disabled>
            <el-option label="男" :value="0" />
            <el-option label="女" :value="1" />
          </el-select>
        </el-form-item>
        <el-form-item label="审核" :label-width="formLabelWidth">
          <el-switch
            v-model="form.userStatus"
            active-text="通过"
            inactive-text="驳回"
            :active-value="1"
            :inactive-value="2"
          />
        </el-form-item>

        <el-form-item label="驳回原因" :label-width="formLabelWidth">
          <el-input v-model="form.reason" type="textarea" autocomplete="off" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="update()"> 审核 </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
const tableData = ref([]);
const multipleSelection = ref<any[]>([]);

import { onMounted, ref } from "vue";
import {
  selectList,
  updateFile,
  deleteById,
  deleteAll,
  checkUser,
} from "../api/api";
import type { ComponentSize } from "element-plus";
import type { UploadProps } from "element-plus";
const handleSelectionChange = (val: any[]) => {
  multipleSelection.value = val;
  console.log(multipleSelection.value);
};

// const selectable = (row: any) => ![1, 2].includes(row.id);
const imageUrl = ref("");
import { ElMessage, ElMessageBox } from "element-plus";
import { load } from "@amap/amap-jsapi-loader";
const handleAvatarSuccess: UploadProps["onSuccess"] = (
  response,
  uploadFile
) => {
  imageUrl.value = response.data;
};

const openUpdate = (any: any) => {
  Object.assign(form.value, any);
  dialogFormVisible.value = true;
};

const headers = ref({
  token: sessionStorage.getItem("token") || "",
});

const form = ref({
  createTime: "",
  gender: null,
  nickName: "",
  reason: "",
  token: "",
  userAvatar: "",
  userEmail: "",
  userMobile: "",
  userName: "",
  userPass: "",
  userStatus: "",
});

const handleClick = (any: any) => {
  dialogFormVisible.value = true;
  Object.assign(form.value, any);
  imageUrl.value = form.value.userAvatar;
};

const update = () => {
  //  驳回必须填写驳回理由
  if (form.value.userStatus == 2) {
    if (form.value.reason == null || form.value.reason == "") {
      ElMessage.error("请填写驳回原因");
      return;
    }
  }
  // 提交审核
  checkUser(form.value).then((res) => {
    console.log(res);
    if (res.data.code == 200) {
      ElMessage.success("审核成功");
      dialogFormVisible.value = false;
      listHh();
    } else {
      ElMessage.error(res.data.msg);
    }
  });
};

const from = ref({
  beginTime: "",
  endTime: "",
  gender: null,
  keyWord: "",
  pageNum: 1,
  pageSize: 2,
});
const total = ref(0);
const size = ref<ComponentSize>("default");
const background = ref(false);
const disabled = ref(false);
const dialogFormVisible = ref(false);
const formLabelWidth = "140px";

const handleSizeChange = (val: number) => {
  console.log(`${val} items per page`);
  from.value.pageSize = val;
  listHh();
};
const handleCurrentChange = (val: number) => {
  console.log(`current page: ${val}`);
  from.value.pageNum = val;
  listHh();
};

const deleteFrom = (userId: number) => {
  deleteById(userId).then((res) => {
    ElMessageBox.confirm("您确定要删除吗?", "警告", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    })
      .then(() => {
        //
        if (res.data.code == 200) {
          ElMessage.success("删除成功");
          listHh();
        } else {
          ElMessage.error(res.data.msg);
        }
      })
      .catch(() => {});
  });
};

/**
 * 批量删除
 */

const deleteAllFrom = () => {
  deleteAll(multipleSelection.value).then((res) => {
    ElMessageBox.confirm("您确定要删除吗?", "警告", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    })
      .then(() => {
        //
        if (res.data.code == 200) {
          ElMessage.success("删除成功");
          listHh();
        } else {
          ElMessage.error(res.data.msg);
        }
      })
      .catch(() => {});
  });
};

const listHh = () => {
  selectList(from.value).then((res) => {
    if (res.data.code == 200) {
      tableData.value = res.data.data.records;
      total.value = res.data.data.total;
    }
  });
};
listHh();
</script>
<style scoped>
.demo-pagination-block + .demo-pagination-block {
  margin-top: 10px;
}
.demo-pagination-block .demonstration {
  margin-bottom: 16px;
}
</style>
<style scoped>
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>
